package pers.cz.config;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * @program: PostGirl-panent
 * @description: 验证码配置
 * @author: Cheng Zhi
 * @create: 2022-06-20 09:42
 **/
@Configuration
@ConfigurationProperties(prefix = "kaptcha")
public class CaptchaConfig {
    @Value("${kaptcha.border}")
    private String border;
    @Value("${kaptcha.border.color}")
    private String borderColor;
    @Value("${kaptcha.image.width}")
    private String imageWidth;
    @Value("${kaptcha.image.height}")
    private String imageHeight;
    @Value("${kaptcha.session.key}")
    private String sessionKey;
    @Value("${kaptcha.textproducer.font.color}")
    private String fontColor;
    @Value("${kaptcha.textproducer.font.size}")
    private String fontSize;
    @Value("${kaptcha.textproducer.char.length}")
    private String charLength;
    @Value("${kaptcha.textproducer.font.names}")
    private String fontNames;

    @Bean(name="captchaProducer")
    public DefaultKaptcha getKaptcheBean() {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties props = new Properties();
        props.setProperty("kaptcha.border",border);
        props.setProperty("kaptcha.border.color",borderColor);
        props.setProperty("kaptcha.image.width",imageWidth);
        props.setProperty("kaptcha.image.height",imageHeight);
        props.setProperty("kaptcha.session.key",sessionKey);
        props.setProperty("kaptcha.textproducer.font.color",fontColor);
        props.setProperty("kaptcha.textproducer.font.size",fontSize);
        props.setProperty("kaptcha.textproducer.char.length",charLength);
        props.setProperty("kaptcha.textproducer.font.names",fontNames);
        defaultKaptcha.setConfig(new Config(props));

        return defaultKaptcha;
    }

}
